/*
 *  DebugLogProcessor.java
 *  Copyright (c) 2010 Omnigon Communications, LLC.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Omnigon
 *  Communications, LLC ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with Omnigon Communications, LLC.
 */
package com.perlak.camel;

import org.apache.camel.Exchange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Processor to log details of processing.
 * @author mateusz.perlak
 */
public final class DebugLogProcessor extends BaseProcessor {

  /**
   * Message to log.
   */
  private String message;
  /**
   * Body attach.
   */
  private boolean bodyAttach;

  /**
   * Create DebugLogProcessor with log message.
   * @param logMessage message to log
   */
  public DebugLogProcessor(final String logMessage) {
    message = logMessage;
  }

  /**
   * Create DebugLogProcessor with log message and attaches body.
   * @param logMessage log message
   * @param attachBody is body attached to log.
   */
  public DebugLogProcessor(final String logMessage, final boolean attachBody) {
    message = logMessage;
    bodyAttach = attachBody;
  }

  /**
   * Processing debug log.
   * @param exchange exchange message
   */
  @Override
  public void processNoError(final Exchange exchange) {
    String body = "";
    if (bodyAttach) {
      if (exchange.getIn().getBody() != null) {
        body = "body:" + exchange.getIn().getBody().toString();
      }
    }
    getLog().debug("DLP: " + message + body);
    exchange.setOut(exchange.getIn());
  }

  /**
   * Gets logger for this class.
   * @return logger
   */
  @Override
  public Logger getLog() {
    return LoggerFactory.getLogger(DebugLogProcessor.class);
  }
}